<script>
	var siteinfo = require('siteinfo.js');
	export default {

		globalData: {
			systemInfo: '',
			siteInfo: require('siteinfo.js'),
			shop_name: '',
			userInfo: null,
			
			api_host: '',
			api_path: '?c=entry&a=wxapp&m=sm_shop&do=api',
			i: 0,
			location_key: '',
			shop_phone: '',
			shop_wechat: '',
			shop_qq: '',
			float_nav_show: 0,
			logged: 0,
			diy_home: 0,
			app_id:'',
			getSystemInfo: function() {

				let t = this;
				uni.getSystemInfo({
					success: function(res) {
						t.systemInfo = res;
					}
				});
			},

			initGlobalData: function() {
				this.i = this.siteInfo.uniacid;
				this.api_host = this.siteInfo.siteroot;
				var t = this;
				
				var url = '&r=home.init_setting';
				
				this.request(url, {}, 'GET', res => {
					
					t.shop_name = res.data.shop_name ? res.data.shop_name : t.shop_name;
					t.location_key = res.data.location_key ? res.data.location_key : '';
					t.shop_phone = res.data.shop_phone ? res.data.shop_phone : '';
					t.shop_wechat = res.data.shop_wechat ? res.data.shop_wechat : '';
					t.shop_qq = res.data.shop_qq ? res.data.shop_qq : '';
					t.float_nav_show = res.data.float_nav_show ? res.data.float_nav_show : 0;
					t.customer_bg_img = res.data.customer_bg_img;
					t.customer_entry_box = res.data.customer_entry_box;
					// t.logged = 1;
					t.app_id = res.data.app_id;
					uni.setNavigationBarTitle({
						title: t.shop_name
					});
				});
				
			},
			checkUserStatus: function( callback, gotoLogin ) {
				var url = '&r=member.xcx.getCustomer';

				this.request(url, {}, 'GET', res => {

					// if (!res.data.telephone && gotoLogin ) {
					if (!res.data.customer && gotoLogin ) {
						uni.navigateTo({
							url: '/sm_shop/pages/user/login'
						});
					}else if( typeof callback == 'function'){
						callback( res );
					}
				});
			},
			getCustomer: function(callback) {

				var url ='&r=member.xcx.getCustomer';

				this.request(url, {}, 'GET', callback);

			},
			request: function(url, data, methods, callback) {
				methods = methods.toUpperCase();
				var api_url = this.api_host + this.api_path;
				api_url += '&i=' + this.i;
				var openid = uni.getStorageSync('openid')
				api_url += '&openid=' + openid;
				url = url.indexOf('&') != 0 ? '&' + url : url;
				api_url += url;

				uni.request({
					url: api_url,
					data: data,
					method: methods ? methods : 'GET',
					success: res => {
					console.log('REQUEST:url', api_url )
					console.log('REQUEST:data', JSON.stringify(data) )
					console.log('REQUEST:res', res )
						if (typeof callback == 'function') {

							callback(res);
						}

					}
				});
			},
			
			/**
			 * 微信小程序登录
			 */
			xcx_login:function( ){
				
				var t = this;
				uni.getProvider({
					service: 'oauth',
					success: function(provider_res) {
						console.log('res.provider：',provider_res.provider);
						if (~provider_res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: uni_login_res => {
									console.log('uni.login_xcx：',uni_login_res);
									// 发送 res.code 到后台换取 openId, sessionKey, unionId
				
									var openid = uni.getStorageSync('openid');
									var session_key = uni.getStorageSync('session_key');
				
				
				
									var url = t.api_host + t.api_path + '&r=member.xcx.login';
									url += '&i=' + t.i;
									url += '&code=' + uni_login_res.code;
									var data = {
										code: uni_login_res.code
									};
									uni.request({
										url: url,
										success: (res) => { console.log('member.xcx.login:',res)
									
											if (res.data.openid) {
												uni.setStorageSync('openid', res.data.openid);
												uni.setStorageSync('session_key', res.data.session_key);
												// t.shop_name = res.data.shop_name ? res.data.shop_name : t.shop_name;
												// t.location_key = res.data.location_key ? res.data.location_key : '';
												// t.shop_phone = res.data.shop_phone ? res.data.shop_phone : '';
												// t.shop_wechat = res.data.shop_wechat ? res.data.shop_wechat : '';
												// t.shop_qq = res.data.shop_qq ? res.data.shop_qq : '';
												// t.float_nav_show = res.data.float_nav_show ? res.data.float_nav_show : 0;
												// t.customer_bg_img = res.data.customer_bg_img;
												// t.customer_entry_box = res.data.customer_entry_box;
												// t.logged = 1;
												// t.app_id = res.data.app_id;
												// uni.setNavigationBarTitle({
												// 	title: t.shop_name
												// });
											}
										}
									});
									
									
								}
							});
						}
					}
				});
			},
			
			/**
			 * 应用登录
			 */
			app_login:function( callback ){
				
				var openid = uni.getStorageSync('openid');
				var app_wx_userinfo = uni.getStorageSync('app_wx_userinfo');
				
				
				
				if( !openid ){
					var t = this;
					uni.getProvider({
						service: 'oauth',
						success: function(provider_res) {
							console.log('res.provider：',provider_res.provider);
							if (~provider_res.provider.indexOf('weixin')) {
								uni.login({
									provider: 'weixin',
									success: uni_login_res => {
										console.log('uni.login_app：',uni_login_res);
										// 发送 res.code 到后台换取 openId, sessionKey, unionId
					
										uni.setStorageSync('openid', uni_login_res.authResult.openid);										
										// uni.setStorageSync('app_wx_userinfo', uni_login_res.authResult);										
										
										if( typeof callback == 'function' ){
											
											callback( uni_login_res.authResult )
										}
									},
									fail: function(res) {
										console.log("App微信获取用户信息失败", res);
									}
								});
							}
						}
					});
				}else{
					if( typeof callback == 'function' ){						
						callback( openid )
					}
				}
				
			},
			app_register:function(){
				
				var t = this;
				this.app_login( res=>{	
					uni.getUserInfo({
						provider: 'weixin',
						success: function(infoRes) {
							console.log('-------获取微信用户所有-----');
							console.log(JSON.stringify(infoRes.userInfo));
							if(infoRes.userInfo && infoRes.userInfo.openId ){
								
								infoRes.userInfo.openid = infoRes.userInfo.openId;
								let url = '&r=member.app.register'
								t.request(url, infoRes.userInfo, 'post', res => {
									
									console.log('member.app.register:', res)
									if (!res.data.status) {
										uni.switchTab({
											url: '/sm_shop/pages/home/home'
										});
									} else {
									
										var desc = res.data.description ? res.data.description : '登录异常';
										uni.showToast({
											title: desc,
											icon: 'none',
											duration: 800
										});
									
									}
								});
							}
							
						},
						fail: res => {
							console.log( 'getUserInfo fail:', res );
						}
					});
					
				} );
			}
			
		},



		onLaunch: function() {

			var appInstance = getApp();

			appInstance = appInstance ? appInstance : this.$scope;
			appInstance.globalData.getSystemInfo();
			appInstance.globalData.initGlobalData();
			
			// #ifdef  MP-WEIXIN
			appInstance.globalData.xcx_login( );
			// #endif
			// #ifdef  APP-PLUS
			appInstance.globalData.app_login( );
			// #endif

			// 获取用户信息
			uni.getSetting({
				success: res => {
					if (res.authSetting['scope.userInfo']) {
						// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
						uni.getUserInfo({
							success: res => {
								// 可以将 res 发送给后台解码出 unionId
								this.globalData.userInfo = res.userInfo

								// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
								// 所以此处加入 callback 以防止这种情况
								if (this.userInfoReadyCallback) {
									this.userInfoReadyCallback(res)
								}
							}
						})
					}
				}
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>
<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
</style>
<style lang="less">
	/*每个页面公共css */
	// @import 'we7/resource/style/weui.wxss';

	.page__hd {
		padding: 40px;
	}

	.page__bd {
		padding-bottom: 40px;
	}

	.page__bd_spacing {
		padding-left: 15px;
		padding-right: 15px;
	}

	.page__ft {
		padding-bottom: 10px;
		text-align: center;
	}

	.page__title {
		text-align: left;
		font-size: 20px;
		font-weight: 400;
	}

	.page__desc {
		margin-top: 5px;
		color: #888;
		text-align: left;
		font-size: 14px;
	}

	.we7-bottom-placeholder {
		width: 100%;
		height: 134rpx;
		position: relative;
		bottom: 0;
		z-index: -1
	}

	.we7-bottom {
		display: flex;
		overflow: hidden;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 10rpx 0;
		border-top: 1px solid #000;
		background-color: #fff;
	}

	.we7-bottom.active {
		background-color: #fff;
	}

	.we7-bottom.active navigatorurl {
		background-color: #fff
	}

	.we7-bottom .we7-bottom-item {
		flex: 1;
		padding: 10rpx;
		text-align: center
	}

	.we7-bottom .item-img {
		width: 50rpx;
		height: 50rpx;
		display: inline-block
	}

	.we7-bottom .item-text {
		display: block;
		font-size: 12px;
		line-height: 1
	}

	.goHome {
		position: fixed;
		bottom: 150rpx;
		right: 30rpx;
		width: 50rpx;
		height: 50rpx;
		background-color: #1ba9ba;
		padding: 30rpx;
		border-radius: 50%
	}

	.goHome>image {
		width: 100%
	}

	.text-right {
		text-align: right;
	}

	/********************自定义样式**************************/

	view {
		box-sizing: border-box;
	}

	.container {
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		padding: 200rpx 0;
		box-sizing: border-box;
	}

	.red {
		color: #FF4F4F;
	}

	.arrow {
		border-bottom: solid 1px;
		border-right: solid 1px;
		width: 15rpx;
		height: 15rpx;
		transform: rotate(-45deg);
	}

	.text-line-through {
		text-decoration: line-through;
	}

	.clear {
		clear: both;
	}

	.hide {
		display: none !important;
	}

	.m-l20 {
		margin-left: 20rpx;
	}

	.m-l50 {
		margin-left: 50rpx;
	}

	/* 购物车 数量加减框 */
	.price-number-panel {

		width: 100%;
		position: absolute;
		bottom: 0;

		.price {

			float: left;
		}

		.number-panel {
			float: right;

			.jian,
			.jia,
			.number {

				width: 50rpx;
				height: 50rpx;
				line-height: 40rpx;
				background-color: rgb(243, 243, 245);
				text-align: center;
				display: inline-block;
				margin-right: 4rpx;
				float: left;


				&.disable {
					background-color: rgb(247, 248, 250);
				}

			}

			.jian,
			.jia {
				//border:solid 1px #cdcdcd;
				font-size: 40rpx;
			}

			.number {
				font-size: 30rpx;
			}


		}

		.remove {
			border: solid 1rpx #cdcdcd;
			border-radius: 10rpx;
			padding: 0 10rpx;
			float: right;
			height: 50rpx;
			line-height: 48rpx;

		}

		&:after {
			content: "";
			clear: both;
		}
	}

	button {

		&:after {
			border: none;
		}
	}
</style>
